package com.atguigu.gmall.product.service.impl;

import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.mapper.BaseAttrValueMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.mapper.BaseAttrInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author ysy
* @description 针对表【base_attr_info(属性表)】的数据库操作Service实现
* @createDate 2023-10-07 23:08:39
*/
@Service
public class BaseAttrInfoServiceImpl extends ServiceImpl<BaseAttrInfoMapper, BaseAttrInfo>
    implements BaseAttrInfoService{

    @Autowired
    BaseAttrInfoMapper baseAttrInfoMapper;
    @Autowired
    BaseAttrValueMapper baseAttrValueMapper;

    /**
     * 根据前端穿来的分类ID，查询这个分类下的所有平属性名和值
     * @param categoryId1 一级分类
     * @param categoryId2 二级分类
     * @param categoryId3 三级分类
     * @return
     */
    @Override
    public List<BaseAttrInfo> getAttrsAndValueByCategory(Long categoryId1, Long categoryId2, Long categoryId3) {
            List<BaseAttrInfo> baseAttrInfos = baseAttrInfoMapper.
                    getAttrsAndValueByCategory(categoryId1,categoryId2,categoryId3);
        return baseAttrInfos;
    }

    @Override
    public void saveAttrInfo(BaseAttrInfo baseAttrInfo) {
        if (baseAttrInfo.getId() == null){
            //新增
            addAttrInfo(baseAttrInfo);
        }else{
            updateAttrInfo(baseAttrInfo);
        }
    }

    private void updateAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1修改属性名
        baseAttrInfoMapper.updateById(baseAttrInfo);

        //哪些是删除
        //原来有哪几个值62,63,64
        //现在提交来了 63,64
        //delete from base_attr_value where id not in(62,63,64) and attr_id = 13

        //2提取到前端提交的id集合
        List<Long> vids = new ArrayList<>();
        List<BaseAttrValue> valueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue attrValue : valueList) {
            if (attrValue.getId()!=null){
                vids.add(attrValue.getId());
            }
        }

        QueryWrapper<BaseAttrValue> wrapper = new QueryWrapper<>();
        wrapper.eq("attr_id",baseAttrInfo.getId());
        if (vids.size()>0){
            wrapper.notIn("id",vids);
        }

        baseAttrValueMapper.delete(wrapper);


        //3修改属性值
        for (BaseAttrValue baseAttrValue : valueList) {
            if (baseAttrValue.getId()!=null){
                //这些是修改
                baseAttrValueMapper.updateById(baseAttrValue);
            }else{
                //这些是新增
                baseAttrValue.setAttrId(baseAttrInfo.getId());
                baseAttrValueMapper.insert(baseAttrValue);
            }
        }
        //哪些是修改
        //哪些是新增
        //哪些是删除

    }

    private void addAttrInfo(BaseAttrInfo baseAttrInfo) {
        //1,属性名信息保存到base_attr_info
        int insert = baseAttrInfoMapper.insert(baseAttrInfo);
        //自增id，会有mybatis自动保存后填入
        Long id = baseAttrInfo.getId();

        //2,属性值信息保存到base_attr_value
        List<BaseAttrValue> attrValueList = baseAttrInfo.getAttrValueList();
        for (BaseAttrValue attrValue : attrValueList) {
            //回填属性id
            attrValue.setAttrId(id);
            baseAttrValueMapper.insert(attrValue);
        }
    }
}




